/**
 * Created by chocolate on 16/5/11.
 */
//var API_BASE_PATH = "http://192.168.0.103:8080/bb";
var API_BASE_PATH = "http://120.26.37.159:8080/bb";
var API_LOGIN_CHECK             = API_BASE_PATH + "/user/login?";       //家长登录申请
var API_GET_CHILDS              = API_BASE_PATH + "/user/childs?";      //获取所有孩子信息
var API_UPLOAD_AVATAR           = API_BASE_PATH + "/user/uploadAvatar?"; //上传用户头像
var API_GET_EVENT               = API_BASE_PATH + "/event/get?";        //获取所有孩子的BB记录
var API_SET_EVENT               = API_BASE_PATH + "/event/set?";        //获取所有孩子的BB记录
var API_CLEAR_EVENT             = API_BASE_PATH + "/event/clear?";        //获取所有孩子的BB记录
var API_REGISTER                = API_BASE_PATH + "/user/register?";    //家长注册申请
var API_BINDING                 = API_BASE_PATH + "/user/bind?";        //家长绑定孩子申请
var API_GET_NOTICES             = API_BASE_PATH + "/publish/get?";      //获取学校公告列表
var API_GET_LEAVES_APPLIES      = API_BASE_PATH + "/leave/get?" ;       //获取请假申请
var API_APPLY_LEAVE             = API_BASE_PATH + "/leave/apply?" ;     //提交请假申请
var API_CHAT_ADDRESS_LIST       = API_BASE_PATH + "/chat/addresslist?"; //发送聊天信息
var API_LATEST_CONTACTOR_LIST   = API_BASE_PATH + "/chat/latestContactor?"; 
var API_USER_ADDRESS_LIST       = API_BASE_PATH + "/chat/userlist?"; 
var API_CHAT_SEND_MSG           = API_BASE_PATH + "/chat/send?";        //发送聊天信息
var API_CHAT_READ_LATEST_NEW    = API_BASE_PATH + "/chat/readnew?";     //读取最新未读消息
var API_CHAT_READ_LATEST        = API_BASE_PATH + "/chat/readlatest?";  //读取最新20条消息（包括已读和未读，包括发送和接受）
var API_CHAT_LATEST_GROUP_MSG   = API_BASE_PATH + "/chat/latestGroupMsg?";
var API_GET_COURSE              = API_BASE_PATH + "/course/get?";       //获取周几课程列表

//单例模式
class Query{
    instance = null;
    constructor(){
        if(typeof instance === 'object')
        {
            return instance;
        }
        else
        {
            instance = this;
            return instance;
        }
    }
    excuteQuery(queryStr,owner,handleResponse)
    {
        console.log(queryStr);
        fetch(queryStr)
            .then(response =>
                response.json())
            .then(json =>
                handleResponse(owner,json))
            .catch(error =>
                console.log("Got Error:"+error)
            );
    }

    /**
     * 查询指定的用户名和密码是否允许登录
     * @param phone
     * @param password
     * @param owner
     * @param callback
     */
    loginCheckQuery(phone,password,owner,callback)
    {
        var queryStr = API_LOGIN_CHECK + 'phone=' +
        encodeURIComponent(phone) + '&password=' + encodeURIComponent(password);
        this.excuteQuery(queryStr,owner,callback);
    }

    /**
     * 根据手机号码查找家长绑定的所有孩子
     * @param phone
     * @param owner
     * @param callback
     */
    getChildsQuery(phone,owner,callback)
    {
        var queryStr = API_GET_CHILDS + 'phone=' + encodeURIComponent(phone);
        this.excuteQuery(queryStr,owner,callback);
    }

    /**
     * 上传用户头像
     * @param phone
     * @param owner
     * @param callback
     */
    uploadAvatar(phone,image,owner,callback)
    {
      console.log('上传用户头像。大小为:' + image.length);
      var queryStr = API_UPLOAD_AVATAR + 'phone=' + encodeURIComponent(phone) + '&image=' + encodeURIComponent(image);
      this.excuteQuery(queryStr,owner,callback);
    }

    /**
     * 根据bb卡号和日期查找bb事件
     * @param bbid
     * @param date
     * @param owner
     * @param callback
     */
    getEventQuery(bbid,date,owner,callback)
    {
        var queryStr = API_GET_EVENT + 'bbid=' + encodeURIComponent(bbid) + '&date=' + encodeURIComponent(date);
        this.excuteQuery(queryStr,owner,callback);
    }

    /**
     * 增加模拟事件
     * @param bbid
     * @param date
     * @param owner
     * @param callback
     */
    submitEvent(bbid,date,type,owner,callback)
    {
        var queryStr = API_SET_EVENT + 'bbid=' + encodeURIComponent(bbid) + '&date=' + encodeURIComponent(date) + 
                  '&type=' + encodeURIComponent(type) ;
        this.excuteQuery(queryStr,owner,callback);
    }

    /**
     * 清除模拟事件
     * @param bbid
     * @param date
     * @param owner
     * @param callback
     */
    clearEvent(bbid,date,owner,callback)
    {
        var queryStr = API_CLEAR_EVENT + 'bbid=' + encodeURIComponent(bbid) + '&date=' + encodeURIComponent(date);
        this.excuteQuery(queryStr,owner,callback);
    }

    /**
     * 家长注册
     * @param phone
     * @param authcode
     * @param password
     * @param owner
     * @param callback
     */
    registerUser(phone,authcode,password,owner,callback)
    {
        var queryStr = API_REGISTER + 'phone=' + encodeURIComponent(phone) + '&authcode=' + encodeURIComponent(authcode)
            + '&password=' + encodeURIComponent(password);
        this.excuteQuery(queryStr,owner,callback);
    }

    /**
     * 绑定BB卡号
     * @param phone
     * @param name
     * @param childname
     * @param bbid
     * @param relation
     * @param owner
     * @param callback
     */
    bindBbCard(phone,name,childname,bbid,relation,owner,callback)
    {
        var queryStr = API_BINDING + 'phone=' + encodeURIComponent(phone) + '&name=' + encodeURIComponent(name)
            + '&childname=' + encodeURIComponent(childname) + '&bbid=' + encodeURIComponent(bbid)
            + '&relation=' + encodeURIComponent(relation) ;
        this.excuteQuery(queryStr,owner,callback);
    }

    /**
     * 获取学校公告
     * @param owner
     * @param callback
     */
    getNotices(owner,callback)
    {
        var queryStr = API_GET_NOTICES;
        this.excuteQuery(queryStr,owner,callback);
    }

    /**
     *获取请假申请
     * @param owner
     * @param callback
     */
    getLeaveApplies(parentPhone,owner,callback)
    {
      var queryStr = API_GET_LEAVES_APPLIES + 'parentPhone=' + encodeURIComponent(parentPhone);
      console.log("向服务器查询请假列表:"+queryStr);
      this.excuteQuery(queryStr,owner,callback);
    }

  /**
   *提交请假申请
   * @param owner
   * @param callback
   */
  applyOneLeave(parentPhone,studentId,applyTime,reason,startTime,endTime,detail,approver,owner,callback)
  {
    var queryStr = API_APPLY_LEAVE + 'parentPhone=' + encodeURIComponent(parentPhone) + '&studentId=' + encodeURIComponent(studentId)
      + '&applyTime=' + encodeURIComponent(applyTime) + '&reason=' + encodeURIComponent(reason)
      + '&startTime=' + encodeURIComponent(startTime) + '&endTime=' + encodeURIComponent(endTime)
      + '&detail=' + encodeURIComponent(detail) + '&approver=' + encodeURIComponent(approver);
    console.log("向服务器提交请假申请:"+queryStr);
    this.excuteQuery(queryStr,owner,callback);
  }


  /**
   *向服务器提交聊天信息
   * @param owner
   * @param callback
   */
  getAddressList(myPhone,owner,callback)
  {
    var queryStr = API_CHAT_ADDRESS_LIST + 'myPhone=' + encodeURIComponent(myPhone);
    console.log("向服务器获取通讯录列表:"+queryStr);
    this.excuteQuery(queryStr,owner,callback);
  }

  getUserList(myPhone,owner,callback)
  {
    var queryStr = API_USER_ADDRESS_LIST + 'myPhone=' + encodeURIComponent(myPhone);
    console.log("向服务器获取通讯录列表:"+queryStr);
    this.excuteQuery(queryStr,owner,callback);
  }

  getLatestContactor(myPhone,owner,callback){
    var queryStr = API_LATEST_CONTACTOR_LIST + 'myPhone=' + encodeURIComponent(myPhone);
    console.log("向服务器获取最近联系人列表:"+queryStr);
    this.excuteQuery(queryStr,owner,callback);
  }

   /**
   *向服务器提交聊天信息
   * @param owner
   * @param callback
   */
  sendChatMsg(myPhone,yourPhone,text,date,owner,callback)
  {
    var queryStr = API_CHAT_SEND_MSG + 'myPhone=' + encodeURIComponent(myPhone) + '&yourPhone=' + encodeURIComponent(yourPhone)
      + '&text=' + encodeURIComponent(text) + '&date=' + encodeURIComponent(date) + '&status=new';
    console.log("向服务器发送聊天信息:"+queryStr);
    this.excuteQuery(queryStr,owner,callback);
  }

   /**
   *从服务器获取最新未读消息（接收方）
   * @param owner
   * @param callback
   */
  readChatLatestNewMsg(myPhone,yourPhone,owner,callback)
  {
    var queryStr = API_CHAT_READ_LATEST_NEW + 'myPhone=' + encodeURIComponent(myPhone) 
        + '&yourPhone=' + encodeURIComponent(yourPhone);
    console.log("从服务器读取最新未读聊天信息:"+queryStr);
    this.excuteQuery(queryStr,owner,callback);
  }

   /**
   *从服务器获取最新聊天消息（接受、发送双方。用于刚进入聊天界面，获取之前的历史消息）
   * @param owner
   * @param callback
   */
  readChatLatestMsg(myPhone,yourPhone,msgnum,owner,callback)
  {
    var queryStr = API_CHAT_READ_LATEST + 'myPhone=' + encodeURIComponent(myPhone) 
        + '&yourPhone=' + encodeURIComponent(yourPhone) + '&msgnum=' + encodeURIComponent(msgnum);
    console.log("从服务器读取最新n条聊天信息:"+queryStr);
    this.excuteQuery(queryStr,owner,callback);
  }

  readChatLatestGroupMsg(myPhone,yourPhone,startDate,msgnum,owner,callback)
  {
    var queryStr = API_CHAT_LATEST_GROUP_MSG 
        + 'myPhone=' + encodeURIComponent(myPhone) 
        + '&yourPhone=' + encodeURIComponent(yourPhone) 
        + '&startDate=' + encodeURIComponent(startDate)  //有多大概率，两个人同时发送群组消息？
        + '&msgnum=' + encodeURIComponent(msgnum);
    console.log("从服务器读取最新n条群聊信息:"+queryStr);
    this.excuteQuery(queryStr,owner,callback);
  }

   /**
   *从服务器获取当天课程列表
   * @param owner
   * @param callback
   */
  getCourse(id,weekDay,owner,callback)
  {
    var queryStr = API_GET_COURSE + 'id=' + encodeURIComponent(id) 
        + '&weekDay=' + encodeURIComponent(weekDay);
    console.log("从服务器读取周"+weekDay+"的课程列表"+queryStr);
    this.excuteQuery(queryStr,owner,callback);
  }

}

module.exports.query = new Query();
